package com.interview.javabasic.socket;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Scanner;

public class UDPServer {
    public static void main(String[] args) throws Exception{
        //服务器端接收客户端发送的数据报
        DatagramSocket socket = new DatagramSocket(65001);//监听的端口号
        byte[] buff = new byte[100];//存储从客户端接收到的内容
        DatagramPacket packet = new DatagramPacket(buff,buff.length);
        //接收客户端发送过来的信息，并将内容封装进DatagramPacket对象中
        socket.receive(packet);

        byte[] data = packet.getData();//从DataGramPacket中获取到真正存储的数据
        //将数据从二进制转化成字符串形式
        String content = new String(data,0,packet.getLength());
        System.out.println(content);
        //将要发送给客户端的数据转化为二进制
        Scanner scanner = new Scanner(System.in);
        String serverwords = scanner.next();
        byte[] sendedContent = String.valueOf("Server: "+ serverwords).getBytes();
        //服务器端给数据端发送数据包
        //从DataGramPacket对象中获取到数据来源地址与端口号
        DatagramPacket packetToClient = new DatagramPacket(sendedContent,sendedContent.length,packet.getAddress(),packet.getPort());
        socket.send(packetToClient);//发送数据给客户端



    }
}
